<?php

namespace framework\system\kernel;

use framework;

/**
 * Description of CRequest
 *
 * @author xiaolei
 */
class Request extends Component {

    public function init() {
        //开启全局缓冲区，避免Header错误
        ob_start();
    }

    public function start() {

        //print_r($_SERVER);
        $time_zone = framework::cm()->get("com.config")->get('application.timezone');
        if ($time_zone) {
            $this->setTimeZone($time_zone);
        }
        /*
         * 
         *
          echo $this->getHttpRoot() . "<br/>";
          echo $this->getHost() . "<br/>";
          echo $this->getDocumentRoot() . "<br/>";
          echo $this->getProtocol() . "<br/>";
          echo $this->getScriptName() . "<br/>";
          print_r($_SERVER);
          echo "request<br/>";
         * 
         */
    }

    public function end() {
        ;
    }

    public function setTimeZone($time_zone) {

        date_default_timezone_set($time_zone);
    }

    public function getHttpRoot() {
        return $this->getSiteRoot();
    }

    public function getSiteRoot() {
        $documentRoot = realpath($this->getDocumentRoot());
        $htdocs = realpath(DIR_HTDOCS);
        $documentRoot = str_replace('\\', '/', $documentRoot);
        $htdocs = str_replace('\\', '/', $htdocs);
        return str_replace($documentRoot, "", $htdocs);
    }

    public function getClientIP($IPLevel = 'D') {
        $clientIP = $_SERVER["REMOTE_ADDR"];

        return $clientIP;
    }

    public function getHost() {
        return isset($_SERVER["HTTP_HOST"]) ? $_SERVER["HTTP_HOST"] : NULL;
    }

    public function getDocumentRoot() {
        $documentRoot = isset($_SERVER["DOCUMENT_ROOT"]) ? $_SERVER["DOCUMENT_ROOT"] : (isset($_SERVER["CONTEXT_DOCUMENT_ROOT"]) ? $_SERVER["CONTEXT_DOCUMENT_ROOT"] : NULL);
        str_replace($documentRoot, "\\", "/");
        if (substr($documentRoot, -1, 1) == "/") {
            $documentRoot = substr($documentRoot, 0, -1);
        }
        return $_SERVER["DOCUMENT_ROOT"] = $documentRoot;
    }

    public function getScriptName() {
        return $_SERVER["SCRIPT_NAME"];
    }

    public function getProtocol() {
        if (isset($_SERVER["REQUEST_SCHEME"])) {
            return strstr(strtolower($_SERVER["REQUEST_SCHEME"]), "https") ? "https" : "http";
        }
        if (isset($_SERVER["SERVER_PROTOCOL"])) {
            return strstr(strtolower($_SERVER["SERVER_PROTOCOL"]), "https") ? "https" : "http";
        }
        return "http";
    }

    public function getPathInfo() {

        $path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : null;
        //echo $path_info = str_replace($this->getSiteRoot(), '', $path_info);
        return $path_info;
    }

    public function getReferer() {

        return isset($_SERVER["HTTP_REFERER"]) ? $_SERVER["HTTP_REFERER"] : NULL;
    }

    public function getQueryString() {
        return $_SERVER['QUERY_STRING'];
    }

    public function getRequestURL() {
        $length = strlen($this->getSiteRoot());
        if (isset($_SERVER["REQUEST_URI"])) {
            $temp = explode("?", $_SERVER["REQUEST_URI"]);
            $temp = array_shift($temp);
            if ($_SERVER['SCRIPT_NAME'] == $temp) {
                $cong_request_url = dirname($_SERVER["REQUEST_URI"]);
            } else {
                $cong_request_url = $_SERVER["REQUEST_URI"];
            }

            if (substr($cong_request_url, 0, $length) == $this->getSiteRoot()) {
                $cong_request_url = substr($cong_request_url, $length + 1);
            }
            return $cong_request_url;
        }
        if (framework::$argv) {
            return framework::getOption('-rurl');
        }
    }

    public function getServerIP() {
        return $_SERVER["SERVER_ADDR"];
    }

    public function getRequestTime() {
        if (isset($_SERVER["REQUEST_TIME_FLOAT"])) {
            return $_SERVER["REQUEST_TIME_FLOAT"];
        }
        return $GLOBALS['cong_start_time'];
    }

    public function get($get = "get.null") {

        $_get = explode(".", $get);
        $type = isset($_get[0]) ? $_get[0] : null;
        $name = isset($_get[1]) ? $_get[1] : null;
        if (empty($type))
            return NULL;
        /**
         * @var return $_r 
         */
        $_r = NULL;
        if ("get" == $type) {
            $_r = isset($_GET[$name]) ? str_replace('$', '', $_GET[$name]) : null;
        }
        if ("post" == $type) {
            $_r = isset($_POST[$name]) ? $_POST[$name] : null;
        }
        if ("cookie" == $type) {
            $_r = isset($_COOKIE[$name]) ? $_COOKIE[$name] : NULL;
        }
        return $_r;
    }

    public function set($set, $value) {

        $_set = explode(".", $set)[0];
        $type = isset($_set[0]) ? $_set[0] : null;
        $name = isset($_set[1]) ? $_set[1] : null;
        if (empty($type))
            return NULL;
        /**
         * @var return $_r 
         */
        $_r = NULL;
        if ("get" == $type) {
            $_GET[$name] = $value;
        }
        if ("post" == $type) {
            $_POST[$name] = $value;
        }
        if ("cookie" == $type) {
            $_COOKIE[$name] = $value;
        }
    }

}
